package org.example

import scala.io.Source

object test2 {
  def main(args:Array[String]): Unit = {
    //scala常用的数据处理方法
    //1.map映射
    def addNum: Int => Int = (x:Int) => x * 2
    val list1 = List(100,200,300).map(addNum)
    val list2 = List(100,200,300).map(x => x * 2)
    val list3 = List(100,200,300).map(_*2)
    println(list3)
    val s = "pan"
    println(s.reverse)
    val wordlist = List("java","scala","cba").map(y => y.reverse)
    println(wordlist)
    val silingqi = List("panpan","hanhan","yunguitou").map(y => y.reverse)
    println(silingqi)

    val score = Array(("盼盼",Array(5.5,6.6,7.7,8.8,8.0)),("云贵",Array(6.0,6.5,7.0,7.5,9.0)))
      score.map(x => (x._1,x._2.max)).foreach(println)
      score.map(tp => (tp._1,tp._2.map(t => if (t >= 8.0) 1 else 0).sum)).foreach(println)

      score.map(y => (y._1,y._2.map(y => if (y <6.0) 1 else 0).sum)).foreach(println)
      score.map(c => (c._1,c._2.sum / c._2.length)).foreach(println)

      val arr1 = Array(1,2,3,4,5,6)
      arr1.filter(t => t % 2 ==0).foreach(println)

      score.map(tp => (tp._1,tp._2.count(t => t <6.0))).foreach(println)
//flatMap扁平映射
      val arr2 = Array("I","love","you")
      arr2.filter(word => word.reverse == word).foreach(println)

    val wordFlat = List("I an pan","you are han","I love scala")
    val wordSplit = wordFlat.map(_.split(" "))
    val wordFlatten = wordSplit.flatten
    println(wordFlatten)

    val flatmpList =wordFlat.flatMap(_.split(" "))

    //4.groupBy分组
    println(flatmpList.groupBy(_.charAt(0)).toList)
println("------------------------------------------")
    //练习
    //分组
    val wordSam =flatmpList.groupBy(word => word)
    println(wordSam)
  //println(wordFlatten.groupBy(identity).toList)
    //统计
    val counterMap = wordSam.map(tp => (tp._1,tp._2.length))
    println(counterMap)
    //转列表
    val countList =counterMap.toList
    println(countList)
    //排序
    val sortList = countList.sortWith(_._2 > _._2)
    println(sortList)
//取前2
    val takeList = sortList.take(2)
    println(takeList)




    println("---------------------------------------------")




    //val wd=List("My dad was my hero，all throughout my life. When I was very little，he appeared to be so large. In my eyes he could do anything. We all knew he was in charge. He was a man of great strength both physically and mentally，and he found ways with a kiss and songs sung to me as I woke up. Those days I surely miss. He made me feel so special，“Miss America”he would sing. I knew I had my father‘s love. It gave me courage to do everythings.")
    //val wd = Source.fromFile("E:\\yun243scala\\abchello\\src\\word.txt").getLines().toList
    //wd.foreach(print)
    //val wdList =wd.flatMap(_.split(" "))
    //println(wdList.groupBy(_.charAt(0)).toList)
    //分组
    //val fzSam = wdList.groupBy(word => word)
    //println(fzSam)
    //推荐
    //val tjMap = fzSam.map(tp => (tp._1,tp._2.length))
    //println(tjMap)

    //val pxList = tjMap.toList
    //println(pxList)

    //val pxiList = pxList.sortWith(_._2 > _._2)
    //println(pxiList)

    //val qqList =pxiList.take(3)
    //println(qqList)
  }
}
